﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using T.FAS.Runtime.Metadata.Entity;
using T.ServiceComponent.DAC.Service;
using T.ServiceComponent.DataAccessEx.Core;

namespace T.FAS.Runtime.Metadata.Dao
{
    public class MetaDataHistoryDao : DaoService<MetaDataHistory>
    {
        public MetaDataHistoryDao(string db = "TFAS") : base()
        {
            this.DBName = db;
        }

        public override string IDColumnName
        {
            get
            {
                return "ID";
            }
        }

        public override string TableName
        {
            get
            {
                return "Meta_History";
            }
        }
        public override List<string> ExceptColumnName
        {
            get
            {
                return new List<string>()
                {
                    "MetaData"
                };
            }
        }

        public List<MetaDataHistory> QueryMetaDataHistoryList(string batchId, string metaDataId, string projectBranchId)
        {
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            string sqlCondition = string.Format("1=1");
            if (string.IsNullOrEmpty(batchId) == false)
            {
                sqlCondition = string.Format("{0} and BatchID=@BatchID", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@BatchID", TDbDataType.VarChar, batchId));
            }
            if (string.IsNullOrEmpty(metaDataId) == false)
            {
                sqlCondition = string.Format("{0} and MetaDataID=@MetaDataID", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@MetaDataID", TDbDataType.VarChar, metaDataId));
            }
            if (string.IsNullOrEmpty(projectBranchId) == false && projectBranchId != "Main")
            {
                sqlCondition = string.Format("{0} and ProjectBranchId=@ProjectBranchId", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@ProjectBranchId", TDbDataType.VarChar, projectBranchId));
            }
            string sql = string.Format(@"SELECT ID,ProjectBranchID,MetaDataID, MetaDataCode, MetaTypeID, MetaVersionID, MetaVersion, '' as ContentJSON,'' as MetaDataJSON, BatchID, Description, State, Creator, CreateTime,LastModifier,LastModifyTime FROM {0}", TableName);
            var ds = this.DB.ExecuteDataSet(string.Format("{0} WHERE {1} order by LastModifyTime desc", sql, sqlCondition), paramsList.ToArray());
            List<MetaDataHistory> list = new List<MetaDataHistory>();
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    list.Add(ConvertToObject(this.Columns, row));
                }
            }
            return list;
        }

        public void DeleteByBatchID(string batchId)
        {
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            string sqlCondition = "BatchID=@BatchID";
            paramsList.Add(this.DB.MakeInParam("@BatchID", TDbDataType.VarChar, batchId));
            this.DB.ExecSqlStatement(string.Format(@"Delete FROM {0} WHERE {1}", TableName, sqlCondition), paramsList.ToArray());
        }
    }
}
